package com.zzb.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzb.pojo.ResultMessage;

/**
 * 用于封装 服务端---->客户端 的websocket消息 json字符串
 */
public class MessageUtils {
    public static String getMessage(boolean isSystem, String fromName, Object message){
        try {
            ResultMessage resultMessage = new ResultMessage();
            resultMessage.setSystem(isSystem);
            resultMessage.setMessage(message);
            if (fromName != null){
                resultMessage.setFromName(fromName);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(resultMessage);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
